package com.tshopping.service.impl;

import com.tshopping.bean.Page;
import com.tshopping.dao.SearchDao;
import com.tshopping.service.SearchService;
import com.tshopping.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @name: SearchServiceImpl
 * @description: 搜索业务层类
 * @author: Jackeys
 * @date: 2021-02-01 10:29
 */
@Service(value = "searchService")
@Transactional
public class SearchServiceImpl implements SearchService {

    @Autowired
    private SearchDao searchDao;

    @Override
    /**
     * @description 搜索店铺
     * @Param: keyWord
     * @Param: currentPage
     * @return ResponseData
     * @author Jackeys
     * @date 2021-02-01 10:36
     */
    public ResponseData searchStore(String keyWord, int currentPage) {
        ResponseData responseData = ResponseData.ok();
        Page page = new Page();
        //1、当前页数
        page.setCurrentPage(currentPage);

        //2、每页显示的数据
        int pageSize = 10;
        page.setPageSize(pageSize);

        //3、总记录数
        int totalCount = searchDao.selectStoreCountByKeyWords(keyWord);
        page.setTotalCount(totalCount);

        //4、根据总记录数和每页显示的记录数计算总页数
        double tc = totalCount;
        Double num = Math.ceil(tc / pageSize);//向上取整
        page.setTotalPage(num.intValue());

        int start = (currentPage - 1) * pageSize;
        int size = pageSize;
        responseData.putDataValue("page", page);
        responseData.putDataValue("storeList", searchDao.selectStoreByKeyWords(keyWord,start,size));
        return responseData;
    }

    @Override
    /**
     * @description 搜索商品
     * @Param: keyWord
     * @Param: currentPage
     * @return
     * @author Jackeys
     * @date 2021-02-01 10:40
     */
    public ResponseData searchGoods(String keyWord, int currentPage) {
        ResponseData responseData = ResponseData.ok();
        Page page = new Page();
        //1、当前页数
        page.setCurrentPage(currentPage);

        //2、每页显示的数据
        int pageSize = 10;
        page.setPageSize(pageSize);

        //3、总记录数
        int totalCount = searchDao.selectGoodsCountByKeyWords(keyWord);
        page.setTotalCount(totalCount);

        //4、根据总记录数和每页显示的记录数计算总页数
        double tc = totalCount;
        Double num = Math.ceil(tc / pageSize);//向上取整
        page.setTotalPage(num.intValue());

        int start = (currentPage - 1) * pageSize;
        int size = pageSize;
        responseData.putDataValue("page", page);
        responseData.putDataValue("goodsList", searchDao.selectGoodsByKeyWords(keyWord,start,size));
        return responseData;
    }
}
